/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.gov.cemaden.library.dao.pcds;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.hibernate.HibernateUtil;
import br.gov.cemaden.library.interfaces.pcds.EstacaoInterface;
import br.gov.cemaden.library.model.Municipio;
import br.gov.cemaden.library.model.pcds.Estacao;
import br.gov.cemaden.library.model.pcds.Rede;
import br.gov.cemaden.library.model.pcds.TipoEstacao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author alan
 */
public class EstacaoDao extends GenericDao implements EstacaoInterface {    
    
    Session session;
    
    public EstacaoDao(Session session){        
        this.session = session;
    }

    public List<Estacao> findAll() {
        
        Transaction tx = session.beginTransaction();
        List<Estacao> estacoes = null;
        try{
            Query query = session.getNamedQuery("Estacao.findAll");
            estacoes = (List<Estacao>) query.list();
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            throw new HibernateException(ex);
        }
        return estacoes;
    }

    public List<Estacao> findByRede(Rede rede) {
        
        //Transaction tx = session.beginTransaction();
        List<Estacao> estacoes = null;
        try {
            Query query = session.getNamedQuery("Estacao.findByRede").setParameter("rede", rede);
            estacoes = (List<Estacao>) query.list();
            //tx.commit();
        } catch (HibernateException e) {
            //tx.rollback();
            System.out.printf("Erro em %s:%n%s - Causa: %s%n", this.getClass().getName(), e.getMessage(), e.getCause());
            throw new HibernateException(e);
        }
        return estacoes;
    }
    
    public Long countByRede(Rede rede) throws HibernateException {        
        
        Query query = session.getNamedQuery("Estacao.countByRede").setParameter("rede", rede);
        Long quantity = (Long) query.uniqueResult();
        
        return quantity;
    }

    public List<Estacao> findByMunicipio(Municipio municipio) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public List<Estacao> findByTipo(TipoEstacao tipo) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
